package edu.odu.cs.sstore.repository;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import edu.odu.cs.sstore.domain.Course;
import edu.odu.cs.sstore.domain.Document;
import edu.odu.cs.sstore.domain.User;

@Repository
public class DocumentRepositoryImpl implements DocumentRepository {

	@Autowired(required=true)
	private SessionFactory sessionFactory;
	
	public Document findById(Long id) {
		return (Document) this.sessionFactory.getCurrentSession().createQuery(
			"from Document docs where docs.id=?").setParameter(0, id)
			.uniqueResult();
	}

	public List<Document> findByCreator(User creator) {
		return (List<Document>)sessionFactory.getCurrentSession().createQuery(
			"from Document docs where docs.creator=? order by docs.fileName")
			.setParameter(0,creator).list();
	}

	public Document saveOrUpdate(Document document) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(document);
		return document;
	}

	public void deleteDocument(Document document) {
		this.sessionFactory.getCurrentSession().delete(document);
	}

}
